﻿
using System;
using System.Drawing;
using System.Collections.Generic;

namespace GECS.GraphicsUtil {

	/// <summary>
	/// An implementation of animation to animate game fonts. You add some GFonts to
	/// this class and update this. According to the time elapsed, the correct font
	/// is chosen. When you invoke the RenderText() method, it is rendered with the
	/// current font.
	/// </summary>
    public class GFontAnimated {

        private List<GFont> fonts = null;

        private List<int> durations = null;

        private int currFontIndex = 0;

        private int duration = 0;

        /// <summary>
        /// Constructs an empty animated font.
        /// </summary>
        public GFontAnimated() {
            fonts = new List<GFont>();
            durations = new List<int>();
        }

        /// <summary>
        /// Constructs an animated font from an array of fonts and duration.
        /// </summary>
        /// <param name="fonts">The array of fonts.</param>
        /// <param name="duration">The duration of each font (in ms).</param>
        public GFontAnimated(GFont[] fonts, int duration) : this() {
            foreach (GFont font in fonts) {
                AddFont(font, duration);
            }
        }

        /// <summary>
        /// Updates this animation with time.
        /// </summary>
        /// <param name="elapsedTime">The time taken by the previous frame.</param>
        public void Update(long elapsedTime) {
            duration += (int)elapsedTime;
            if (duration >= durations[currFontIndex]) {
                duration = 0;
                currFontIndex++;
                if (currFontIndex >= fonts.Count) {
                    currFontIndex = 0;
                }
            }
        }

        /// <summary>
        /// Renders text with the current font.
        /// </summary>
        /// <param name="txt">The text to be rendered.</param>
        /// <param name="g">The graphics context.</param>
        /// <param name="x">The starting x-position.</param>
        /// <param name="y">The starting y-position.</param>
        public void RenderText(string txt, Graphics g, int x, int y) {
            GetFont().RenderText(txt, g, x, y);
        }

        /// <summary>
        /// Gets the current font in order.
        /// </summary>
        /// <returns>The current font in order.</returns>
        public GFont GetFont() {
            return fonts[currFontIndex];
        }

        /// <summary>
        /// Adds a font to the font list.
        /// </summary>
        /// <param name="font">The GFont object.</param>
        /// <param name="duration">The duration of the font.</param>
        public void AddFont(GFont font, int duration) {
            fonts.Add(font);
            durations.Add(duration);
        }

        /// <summary>
        /// Clones this animation deeply.
        /// </summary>
        /// <returns>The cloned animation.</returns>
        public GFontAnimated Clone() {
            GFontAnimated fanim = new GFontAnimated();
            for (int i = 0; i < fonts.Count ; i++) {
            	fanim.AddFont(fonts[i], durations[i]);
            }
            return fanim;
        }

    }

}
